package com.ruoyi.develop.factory.factoryProduceMain.service.impl;

import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.develop.factory.factoryProduceSale.domain.FactoryProduceSale;
import com.ruoyi.develop.factory.factoryProduceSale.service.IFactoryProduceSaleService;
import com.ruoyi.develop.factory.factoryProduceSaleAll.domain.FactoryProduceSaleAll;
import com.ruoyi.develop.factory.factoryProduceSaleAll.service.IFactoryProduceSaleAllService;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.factory.factoryProduceMain.mapper.FactoryProduceMainMapper;
import com.ruoyi.develop.factory.factoryProduceMain.domain.FactoryProduceMain;
import com.ruoyi.develop.factory.factoryProduceMain.service.IFactoryProduceMainService;

/**
 * 生产订单信息Service业务层处理
 * @author qh
 * @date 2024-11-06
 */
@Service
public class FactoryProduceMainServiceImpl implements IFactoryProduceMainService {
    @Resource
    private FactoryProduceMainMapper factoryProduceMainMapper;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Autowired
    private IFactoryProduceSaleService iFactoryProduceSaleService;
    @Autowired
    private IFactoryProduceSaleAllService iFactoryProduceSaleAllService;
    /**
     * 查询生产订单信息
     * @param id 生产订单信息主键
     * @return 生产订单信息
     */
    @Override
    public FactoryProduceMain selectById(String id) {
        return factoryProduceMainMapper.selectById(id);
    }

    /**
     * 查询生产订单信息列表
     * @param entity 生产订单信息
     * @return 生产订单信息
     */
    @Override
    public List<FactoryProduceMain> selectList(FactoryProduceMain entity) {
        return factoryProduceMainMapper.selectList(entity);
    }

    /**
     * 新增生产订单信息
     * @param entity 生产订单信息
     * @return 结果
     */
    @Override
    public int insert(FactoryProduceMain entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        String poItemId="";
        if(entity.getPoItemIds().length>1){
            for(String id:entity.getPoItemIds()){
                poItemId=poItemId+"&"+id;
            }
            entity.setPoItemId(poItemId.substring(1));
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        factoryProduceMainMapper.insert(entity);

        if(entity.getPoItemIds().length>0){
            for(String id:entity.getPoItemIds()){
                GoodsSale goodsSale=goodsSaleService.selectById(id);
                if(goodsSale!=null){
                    FactoryProduceSale factoryProduceSale=new FactoryProduceSale();
                    factoryProduceSale.setCaigouId(entity.getProjectId());
                    factoryProduceSale.setProduceId(entity.getId());
                    factoryProduceSale.setSaleId(goodsSale.getId());
                    factoryProduceSale.setGoodsId(goodsSale.getGoodsId());
                    factoryProduceSale.setCount(goodsSale.getOrderCount());
                    factoryProduceSale.setCode(goodsSale.getGoodsCode());
                    factoryProduceSale.setName(goodsSale.getGoodsName());
                    factoryProduceSale.setXinghao(goodsSale.getGoodModel());
                    factoryProduceSale.setMiaoshu(goodsSale.getRemark());
                    iFactoryProduceSaleService.insert(factoryProduceSale);
                }
            }
        }
        List<FactoryProduceSale> factoryProduceSaleListGroup = iFactoryProduceSaleService.selectListGroupGoodsId(entity.getId());
        for(FactoryProduceSale factoryProduceSale:factoryProduceSaleListGroup){
            FactoryProduceSaleAll factoryProduceSaleAll = new FactoryProduceSaleAll();
            factoryProduceSaleAll.setCaigouId(factoryProduceSale.getCaigouId());
            factoryProduceSaleAll.setProduceId(factoryProduceSale.getProduceId());
            factoryProduceSaleAll.setGoodsId(factoryProduceSale.getGoodsId());
            factoryProduceSaleAll.setCount(factoryProduceSale.getCount());
            factoryProduceSaleAll.setCode(factoryProduceSale.getCode());
            factoryProduceSaleAll.setName(factoryProduceSale.getName());
            factoryProduceSaleAll.setXinghao(factoryProduceSale.getXinghao());
            factoryProduceSaleAll.setMiaoshu(factoryProduceSale.getMiaoshu());
            iFactoryProduceSaleAllService.insert(factoryProduceSaleAll);
            iFactoryProduceSaleService.updateByGoodsId(entity.getId(),factoryProduceSale.getGoodsId(),factoryProduceSaleAll.getId());
        }
        return 1;
    }

    @Override
    public int insertMain(FactoryProduceMain entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return factoryProduceMainMapper.insert(entity);
    }
    /**
     * 修改生产订单信息
     * @param entity 生产订单信息
     * @return 结果
     */
    @Override
    public int update(FactoryProduceMain entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return factoryProduceMainMapper.update(entity);
    }

    /**
     * 批量删除生产订单信息
     * @param ids 需要删除的生产订单信息主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return factoryProduceMainMapper.deleteByIds(ids);
    }

    /**
     * 删除生产订单信息信息
     * @param id 生产订单信息主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return factoryProduceMainMapper.deleteById(id);
    }
    @Override
    public String selectMaxCode(String today) {
        return factoryProduceMainMapper.selectMaxCode(today);
    }
}
